Register-Lib PowerShell.Logging

# Logging Functions
$global:indentLevel = 0

function Get-TimeStamp {
    get-date -format "T"
}

function Get-Indent($indent){
    $str = ""
    if($indent -gt 0) {
        0..($indent-1) | % { $str += "    " }
    }
    return $str
}


function Log {
    $args[0] = (Get-Indent($global:indentLevel)) + $args[0]  
    write-host (Get-TimeStamp) @args
}

Log "PowerShell.Logging initialized"

function Log-Verbose($message, [switch]$Verbose = ($VerbosePreference -eq 'Continue')) {
    $message = (Get-Indent($global:indentLevel)) + $message
    write-verbose "$(Get-TimeStamp) $message" -Verbose:$Verbose
}

function With-NestedLogging($block, $increment = 1) {
    $global:indentLevel += $increment
    try {
    & $block
    }
    finally {
        $global:indentLevel -= $increment
    }
}


filter Log-Item($Prefix, [ScriptBlock] $Process, $Suffix) {
    if($Process){
        $str = (& $Process $_)
    } else {
        $str = $_
    }
    Log "$($prefix)$($str)$($suffix)"
    $_
}

